/*******************************************************************************
 * Copyright (c) 2005, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package us.gaaoc.framework.dao;

import java.util.Date;

import org.apache.log4j.Logger;

import org.hibernate.Query;
import us.gaaoc.framework.model.CounterPerSigner;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.util.DateUtil;
import us.gaaoc.framework.dao.ModelFacade;

public class CounterPerSignerDAOHibernate extends GenericHibernateDAO<CounterPerSigner, String, CounterPerSignerDAO>
implements  CounterPerSignerDAO{
	
	private static final Logger LOG = Logger.getLogger(CounterPerSignerDAOHibernate.class);

	protected CounterPerSignerDAOHibernate() {}
	
	/*public CounterPerSigner getCounterPerSigner(){
		LOG.info("A query for the counter per signer rows is going to be created .....");
		Query q = this.getSession().createQuery("from CounterPerSigner");
		//q.setString("onlyOneRecord", "counterUuid");
		LOG.info("A query for the counter per signer rows has been created .....");
		return (CounterPerSigner) q.list();
	} */

	public void updateCounterPerSigner(String counterUuid, int counterValue, Date dateOfLastSignature, String personUuid) {
		LOG.info("Query the table CounterPerSigner given a personUuid .....");
		Query q = this.getSession().createQuery("from CounterPerSigner c where c.counterUuid=?");
		q.setString(0, counterUuid);
		CounterPerSigner counterPerSigner = (CounterPerSigner) q.uniqueResult();		
		LOG.info("Setting the counterValue property ....."+new Integer(counterValue).toString());
		counterPerSigner.setCounterValue(counterValue);
		LOG.info("Setting the dateOfLastSignature property ....."+dateOfLastSignature.toString());
		counterPerSigner.setDateOfLastSignature(dateOfLastSignature);
		LOG.info("Setting the person property ....."+personUuid);
		counterPerSigner.setPersonUuid(personUuid);
		LOG.info("persisting the counterPerSigner object .....");
	    this.makePersistent(counterPerSigner);		
	}

	public CounterPerSigner findCounterPerSignerBySignerUuid(String uuid) {
		LOG.info("Query the table for the signer given a personUuid .....");
		Query q = this.getSession().createQuery("from CounterPerSigner c where c.personUuid=?");
		q.setString(0, uuid);
		LOG.info("A query for the signer whose uuid is "+uuid+" in the counterPerSigner returns....");		
		return (CounterPerSigner) q.uniqueResult();
	}

	public boolean isSigner(String uuid) {
		Query q = this.getSession().createQuery("from CounterPerSigner c where c.personUuid=?");
		q.setString(0, uuid);
		if(q.uniqueResult()==null){
		return false;
		}else{
			return true;
		}
	}

	public String findCounterUuidBySignerUuid(String uuid) {
		Query q = this.getSession().createQuery("from CounterPerSigner c where c.personUuid=?");
		q.setString(0, uuid);
		LOG.info("A query for the signer whise uuid is "+uuid+" in the counterPerSigner returns....");
		
		CounterPerSigner cps = (CounterPerSigner) q.uniqueResult();
		String ret = null;
		if(cps != null)
		{
			ret = cps.getCounterUuid();
		}
		
		return ret;
	}

	public void updateCounterPerSigner(String personUuid) {
		LOG.info("Inside the updateCounterPerSigner method of the CounterPerSignerDAOHibernate  .....");
		LOG.info("Creating a query to get the record in the CounterPerSigner for the "+personUuid+".....");
		Query q = this.getSession().createQuery("from CounterPerSigner c where c.personUuid=?");
		q.setString(0, personUuid);
		LOG.info("Incrementing the value of the counter...");
		int newValue = ((CounterPerSigner)q.uniqueResult()).getCounterValue() + 1;
		CounterPerSigner counter = (CounterPerSigner)q.uniqueResult();
		counter.setCounterValue(newValue);
		LOG.info("Setting today's day.....");
		counter.setDateOfLastSignature(DateUtil.getCurrentDate());
		this.makePersistent(counter);
		LOG.info("Persisting the CounterPerSigner with the new values...");
		
	}

	

	public void insertCounterPerSigner(CounterPerSigner counter) {
		LOG.info("Inside the insertCounterPerSigner method of the CounterPerSignerDAOHibernate .....");
		CounterPerSigner c = new CounterPerSigner();
		c.setCounterUuid(counter.getCounterUuid());
		c.setCounterValue(counter.getCounterValue());
		c.setDateOfLastSignature(counter.getDateOfLastSignature());
		c.setPersonUuid(counter.getPersonUuid());
		this.makePersistent(c);
	}

	

	
} 
